package xml;

import java.io.File;
import java.util.HashMap;
import org.w3c.dom.Document;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;

public class Reader extends NodeUtilities implements IReader {

	@Override
	public HashMap<String, String> getPathFromXML(String filename) {
		try {
			HashMap<String, String> map = new HashMap<String, String>();
			File f = new File(filename);
			if (!f.exists()) {
				return map;
			}
			
			
			if (filename.compareTo(defaultPlaylist) == 0) {
				super.setNodeName(nodeNamePlaylist);
			} else {
				super.setNodeName(nodeNameImage);
			}

			Document document =super.getDocument(filename);
			NodeList directory = document.getElementsByTagName(super.getNodeName());
			for (int i = 0; i < directory.getLength(); i++) {
				Node node = directory.item(i);

				String path = node.getAttributes().getNamedItem(attributeName)
						.getNodeValue();
				String name = node.getFirstChild().getNodeValue();
				map.put(path, name);
			}
			return map;
		} catch (Exception e) {
			return null;
		}
	}

}
